package com.yydm.serviceacls.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yydm.serviceacls.entity.Permission;
import com.yydm.serviceacls.entity.RolePermission;
import com.yydm.serviceacls.mapper.PermissionMapper;
import com.yydm.serviceacls.service.PermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yydm.serviceacls.service.RolePermissionService;
import com.yydm.servicebase.exceptionhandler.LException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 权限 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-01-12
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {


    @Autowired
    RolePermissionService roleAndPermissionServer;

    @Override
    public void delete(Long id) {
        //判断当前权限是否有角色关联
        LambdaQueryWrapper<RolePermission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RolePermission::getPermission_id, id);
        long count = roleAndPermissionServer.count(queryWrapper);
        if (count > 0) {
            throw new LException(500, "当前权限有角色关联，不能删除");
        }
        super.removeById(id);
    }
}
